'use strict';

const Service = require('egg').Service;
const ConnectOption = require('../oltmodules/ConnectOption');
const ZTEOLT = require('../oltmodules/ZTEOLT');
const HWOLT = require('../oltmodules/HuaWeiOLT')
const XDOLT = require('../oltmodules/XinDeOLT')
const XGLOLT = require('../oltmodules/XinGeLinOLT');
class PonPortsService extends Service {
  async list(params) {
    let result = {
      count:"",
      rows:""
    }
    const where = {};
    where.oltId = params.OltId
    if(params.search){
      where.port = new RegExp(params.search)
    }
    result.count = await this.ctx.model.PonPorts.find(where).count('_id')
    result.rows = await this.ctx.model.PonPorts.find(where).sort('p').sort('s').skip(parseInt((params.page - 1) * params.limit)).limit(parseInt(params.limit))
    return result
  }
  async listAll(params){
    let oltResult = await this.ctx.model.Olt.findOne({_id:params.OltId})
    let result = await this.ctx.model.PonPorts.find({oltId:params.OltId}).sort('num')
    if(oltResult && oltResult.oltType == 'ZTE'){
      let arr = []
        result.forEach(element => {
          let port = element.port.split('_')[0]+'-onu_'+element.port.split('_')[1]
            arr.push({
              _id:element._id,
              port:port})
        });
        return arr
    }else{
      return result
    }
  }
  async openAllPonPorts(params){
    let result = {
      msg:"",
      flag:false
    }
    const oltInformation = await this.ctx.model.Olt.findOne({_id:params.oltId});
    const ponarr = await this.ctx.model.PonPorts.find({oltId:params.oltId})
    try {
      await openAllPonPorts(ponarr,oltInformation,this)
      await this.ctx.model.PonPorts.updateMany({oltId:params.oltId},{$set:{
        adminStatus:"up"
      }})
      result.msg = "enable all pon ports successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "enable all pon ports fail,"+error
      result.flag = false
      return result
    }
  }
  async getPonPortMsg(params){
    let result = {
      msg:"",
      data:"",
      flag:false
    }
    const portResult = await this.ctx.model.PonPorts.findOne({_id:params.id})
    const oltInformation = await this.ctx.model.Olt.findOne({_id:portResult.oltId});
    try {
      let info = await getPonPortMsg(portResult,oltInformation,this)
      result.msg = "getPortMsg success",
      result.data = info
      result.flag = true
      return result
    } catch (error) {
      result.msg = "getPortMsg fail,"+error
      result.flag = false
      result.data = ""
      return result
    }
  }
  async setPonPortMsg(params){
    let result = {
      msg:"",
      flag:false
    }
    let ponPortResult = await this.ctx.model.PonPorts.findOne({_id:params.id})
    let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltID})
    try {
      await setPonPortMsg(ponPortResult,params.type,params.min,params.max,oltResult,this)
      await this.ctx.model.PonPorts.updateOne({_id:params.id},{$set:{adminStatus:params.type}})
      result.msg = "setPonPort success"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "setPonPort fail,"+error
      result.flag = false
      return result
    }
  }
}
async function openAllPonPorts(ponarr,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        const zte = new ZTEOLT(options);
        result = await zte.openAllPonPorts(ponarr,oltInformation.oltHardVersion,me)
        break;
        case "HUAWEI":
        const hw = new HWOLT(options);
        result = await hw.openAllPonPorts(ponarr,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function getPonPortMsg(portResult,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        const zte = new ZTEOLT(options);
        result = await zte.getPonPortMsg(portResult,oltInformation.oltHardVersion,me)
        break;
        case "HUAWEI":
        const hw = new HWOLT(options);
        result = await hw.getPonPortMsg(portResult,me)
        console.log(result)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function setPonPortMsg(portResult,type,min,max,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result,cmd
    switch (oltInformation.oltType) {
      case "ZTE":
        const zte = new ZTEOLT(options);
        if(type == 'up'){
          cmd = 'no shutdown'
        }else if(type == 'down'){
          cmd = 'shutdown'
        }
        result = await zte.setPonPortMsg(portResult,cmd,min,max,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI": 
        const hw = new HWOLT(options);
        if(type == 'up'){
          cmd = "undo shutdown"
        }else if(type == 'down'){
          cmd = "shutdown"
        }
        result = await hw.setPonPortMsg(portResult,cmd,min,max,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}

module.exports = PonPortsService;
